package pl.pr3sto

import grails.util.Environment
import pl.pr3sto.security.Person
import pl.pr3sto.security.Profile
import pl.pr3sto.security.Role
import pl.pr3sto.auctions.Category

class BootStrapService {

    static transactional = true
	
	// TODO: @KM add test
    void boot(Environment env) {
		createInitialCommonData()
		
		if(env == Environment.DEVELOPMENT) {
			createDevTestData()
		}
    }
	
	void createInitialCommonData() {
		createGuestUser()
		createAdmins()
		createCategories()
	}
	
	void createDevTestData() {
		createUsers()
	}
	
	void createGuestUser() {
		if(Profile.countByRole(Role.GUEST) == 0) {
			Profile.build(role: Role.GUEST, person: Person.build(firstName: "Guest", lastName: "User"))
		}
		log.info("Created guest user")
	}
	
	void createAdmins() {
		Person person = new Person(firstName: "Pr3sto", lastName: "Admin", address: "ul. pr3sto 3", city: "pr3sto", postCode: "33-333", phone: '333333', email: "rego4@gazeta.pl")
		Profile profile = new Profile(login: "admin", password: "admin".encodeAsMD5(), person: person, role: Role.ADMIN)
		person.save()
		profile.save()
		person = new Person(firstName: "Pr3sto", lastName: "Superadmin", address: "ul. pr3sto 3", city: "pr3sto", postCode: "33-333", phone: '333333', email: "rego@gazeta.pl")
		profile = new Profile(login: "god", password: "god".encodeAsMD5(), person: person, role: Role.SUPERADMIN)
		person.save()
		profile.save()		
	}
	
	void createUsers() {
		Person person = new Person(firstName: "Leszek", lastName: "Nowak", address: "ul. Czysta 2", city: "Katowice", postCode: "41-000", phone: '2346356', email: "rego1@gazeta.pl")
		Profile profile = new Profile(login: "user1", password: "user1".encodeAsMD5(), person: person, role: Role.USER)
		person.save()
		profile.save()
		person = new Person(firstName: "Nina", lastName: "Kowalska", address: "ul. Nowoczesna 12", city: "Sosnowiec", postCode: "42-100", phone: '1524356', email: "rego2@gazeta.pl")
		profile = new Profile(login: "user2", password: "user2".encodeAsMD5(), person: person, role: Role.USER)
		person.save()
		profile.save()
		person = new Person(firstName: "Karol", lastName: "Plichta", address: "ul. Sobieskiego 33", city: "Dabrowa Gornicza", postCode: "41-300", phone: '2654344', email: "rego3@gazeta.pl")
		profile = new Profile(login: "user3", password: "user3".encodeAsMD5(), person: person, role: Role.USER)
		person.save()
		profile.save()
	}
	
	void createCategories() {
		Map categories = [
					"category.antiquities": [
						"category.paintings": null,
						"category.furniture": null,
						"category.sculpture": [
							"category.wooden": null,
							"category.stone": null,
							"category.metal": null
						]
					],
					"category.music":[
						"category.intruments": [
							"category.blown": null,
							"category.strings": null,
							"category.keys": null,
							"category.drums": null
						],
						"category.vinyls": [
							"category.rock": null,
							"category.jazz": null,
							"category.pop": null,
							"category.classic": null
						],
						"category.cds": [
							"category.rock": null,
							"category.jazz": null,
							"category.pop": null,
							"category.classic": null
						]
					],
					"category.books": [
						"category.schoolbooks": null,
						"category.guides": null,
						"category.novels": null,
						"category.poetry": null
					],
					"category.vehicles": [
						"category.cars": null,
						"category.trucks": null,
						"category.motorcycles": null
					],
					"category.computers": [
						"category.laptops": null,
						"category.desktops": null,
						"category.software": null,
						"category.parts": [
							"category.mobos": null,
							"category.cpus": null,
							"category.gpus": null,
							"category.rams": null,
							"category.hdds": null,
							"category.drives": null,
							"category.printers": null,
							"category.keyboards": null,
							"category.mice": null
						]
					]
				]
		createBranchOfCategories(null, categories)
	}
	
	private Category createBranchOfCategories(Category parent, Map subcategories) {
		subcategories.each { key, value ->
			Category category = new Category(name: key, parentCategory: parent)
			category.save()
			if (value) {
				createBranchOfCategories(category, value)
			}
		}
	}
}
